package org.spring.springboot.app.sys.office.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.office.domain.bo.AgentTypeOfficeBO;
import org.spring.springboot.app.sys.office.domain.po.SysOfficePO;
import org.spring.springboot.app.sys.office.domain.po.SysOfficeWalletPO;
import org.spring.springboot.app.sys.office.domain.vo.InsertSysOfficeReqVO;
import org.spring.springboot.app.sys.office.domain.vo.SysOfficeAllResVO;
import org.spring.springboot.app.sys.office.domain.vo.SysOfficeWalletVO;
import org.spring.springboot.app.sys.office.service.ISysOfficeService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(description="机构操作接口",tags = ApiIndex.SysOfficeController)
@RequestMapping(value = "/api/sysOffice")
@RestController
@AccessTokenVerify
public class SysOfficeController {

    @Autowired
    private ISysOfficeService sysOfficeService;

    @ApiOperation(value="查询机构信息列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysOfficeAllResVO>> findSysOffice() {
        R<List<SysOfficeAllResVO>> result = new R();
        List<SysOfficeAllResVO> list = sysOfficeService.findAll();
        result.setData(list);
        return result;
    }

    @ApiOperation(value="查询该机构信息列表")
    @RequestMapping(value = "/findsysofficeId", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysOfficeAllResVO>> findSysOfficeId(
            @ApiIgnore User user
    ) {
        R<List<SysOfficeAllResVO>> result = new R();
        String officeid=user.getOfficeId();
        List<SysOfficeAllResVO> list = sysOfficeService.findOfficeId(officeid,null);
        result.setData(list);
        return result;
    }

    @ApiOperation(value="通过机构ID查询机构下的子机构信息")
    @RequestMapping(value = "/findChildrenOfficeByPId", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysOfficeAllResVO>> findChildrenOfficeByPId(
            @ApiParam(value = "父亲机构ID", required = true) @RequestParam String officePId,
            @ApiParam(value = "需要查询的机构类型", required = false) @RequestParam(defaultValue = "") String type
    ) {
        R<List<SysOfficeAllResVO>> result = new R();
        List<SysOfficeAllResVO> list = sysOfficeService.findOfficeChildrenByPId(officePId,type);
        result.setData(list);
        return result;
    }

    @ApiOperation(value="根据机构ID查看该机构下的代理商类型信息")
    @RequestMapping(value = "/findAgentTypeByOfficeId", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<AgentTypeOfficeBO>> findAgentTypeByOfficeId(
            @ApiParam(value = "机构id", required = true) @RequestParam String officeId) {
        R<List<AgentTypeOfficeBO>> result = new R();
        List<AgentTypeOfficeBO> list = sysOfficeService.findAgentTypeByOfficeId(officeId);
        result.setData(list);
        return result;
    }

    @ApiOperation(value="添加机构信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiParam(value = "机构对象", required = true) @RequestBody InsertSysOfficeReqVO sysOffice, BindingResult bindingResult) {
        R result = new R();
        if ( bindingResult.hasErrors() ) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return sysOfficeService.addOffice(sysOffice);
        /*int code=sysOfficeService.save(sysOffice);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return result;*/
    }

    @ApiOperation(value="修改机构信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @AccessTokenVerify
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R update(@ApiParam(value = "机构对象", required = true) @Valid  @RequestBody InsertSysOfficeReqVO sysOffice, BindingResult bindingResult) {
        R result = new R();
        if(  bindingResult.hasErrors() ) {
            result.setCode(1);
            result.setMessage("保存失败");
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        int code=sysOfficeService.update(sysOffice);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return result;
    }

    @ApiOperation(value="删除机构信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @AccessTokenVerify
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(@ApiParam(value = "机构ID", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (id==""||id==null) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("修改失败!机构ID不能为空");
            return result;
        }
        SysOfficePO sysOfficePO = new SysOfficePO();
        sysOfficePO.setId(id);
        int code= sysOfficeService.delete(sysOfficePO);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("修改失败");
        }
        return result;
    }

    @ApiOperation(value="根据类型查询机构信息")
    @RequestMapping(value = "/findSysOfficeByType", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysOfficeAllResVO>> findSysOfficeByType(
    @ApiParam(value = "类型(可逗号拼接)", required = true) @RequestParam("type") String type,
    @ApiParam(value = "父类ID", required = false) @RequestParam(defaultValue = "") String parent_id
    ) {
        R<List<SysOfficeAllResVO>> result = new R();
        if(type==null){
            result.setCode(1000);
            result.setMessage("参数传入错误,type为：1,2,3");
            return result;
        }
        String[] typeStr=type.split("\\,");
        SysOfficePO sysOfficePO=new SysOfficePO();
        if(type.equals("1")){
            sysOfficePO.setType("1");
        }else if(type.equals("2")){
            sysOfficePO.setType("2");
        }
        List<SysOfficeAllResVO> list = sysOfficeService.findAllBySysOffice(typeStr,parent_id);
        result.setData(list);
        return result;
    }



    @ApiOperation(value="查询机构钱包")
    @RequestMapping(value = "/queryWallet", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<SysOfficeWalletVO> queryWallet(
            @ApiIgnore User user
            ) {
        R result = new R();
        SysOfficeWalletPO po = sysOfficeService.queryWallet(user.getOfficeId());
        SysOfficeWalletVO vo = new SysOfficeWalletVO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }
}
